﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.Script.Serialization;
using System.Web.UI.WebControls;

public partial class ServiceTaxDetailMaster : System.Web.UI.Page, ICallbackEventHandler
{
    Dictionary<string, string> ClientRequest = null;
    Dictionary<string, string> ClientResponse = null;
    JavaScriptSerializer serializer;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsCallback)
        {
            CallBackReference();
            if (!IsPostBack)
            {
                if (SessionInfo.prop_getObj.EmployeeID == 0)
                    Response.Redirect("login.aspx");
                else
                {
                    hdnExportToExcelFilePath.Value = GlobalDefaultValues.DHTMLXExportToExcelFilePath;
                    PLMstFormMasters objRights = CommonUtil.CheckRightsForForm("2");
                    if (objRights.ViewAccess == false)
                        Response.Redirect("Login.aspx");
                    cboStatus.Focus();

                    UI_BindDropDown();

                    grdResult.DataSource = new BL_ServiceTax().BL_fnGetServiceTaxDetailList(new PLServiceTax());
                    grdResult.DataBind();
                }
            }
        }
    }

    protected void UI_BindDropDown()
    {
        cboServiceTax.DataSource = new BL_ServiceTax().BL_fnGetServiceTaxBinding();
        cboServiceTax.DataTextField = "ServiceTaxName";
        cboServiceTax.DataValueField = "ServiceTaxID";
        cboServiceTax.DataBind();
        cboServiceTax.Items.Insert(0,new ListItem() { Text = "All Service Taxes", Value = "0" });

        cboAddServiceTax.DataSource = new BL_ServiceTax().BL_fnGetServiceTaxBinding();
        cboAddServiceTax.DataTextField = "ServiceTaxName";
        cboAddServiceTax.DataValueField = "ServiceTaxID";
        cboAddServiceTax.DataBind();
        cboAddServiceTax.Items.Insert(0, new ListItem() { Text = "Select Service Taxe", Value = "0" });
        
    }

    // Override the Render method to ensure that this control
    // is nested in an HtmlForm server control, between a <form runat=server>
    // opening tag and a </form> closing tag.
    protected override void Render(HtmlTextWriter writer)
    {
        // Ensure that the control is nested in a server form.
        if (Page != null)
        {
            Page.VerifyRenderingInServerForm(this);
        }
        base.Render(writer);
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }



    #region callback functions
    public void CallBackReference()
    {
        string strReference = Page.ClientScript.GetCallbackEventReference(this, "args", "getResponse", "context", "ClientCallbackError", true);
        string strScript = " function UseCallback(args,context) { " + strReference + " ; } ";
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UseCallback", strScript, true);
    }

    public string GetCallbackResult()
    {
        System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        oSerializer.MaxJsonLength = GlobalDefaultValues.MaxJsonLength;
        return oSerializer.Serialize(ClientResponse);
    }

    public void RaiseCallbackEvent(string strEventArgument)
    {
        try
        {
            serializer = new JavaScriptSerializer();
            ClientRequest = serializer.Deserialize<Dictionary<string, string>>(strEventArgument);
            ClientResponse = new Dictionary<string, string>();
            string Track = ClientRequest["Track"];
            ClientResponse = new Dictionary<string, string>();
            ClientResponse["Track"] = Track;
            if (SessionInfo.prop_getObj.EmployeeID == 0)
            {
                ClientResponse["SessionTimeOut"] = "1";
            }
            else
            {
                ClientResponse["SessionTimeOut"] = "0";
                if (Track.Equals("GetSearchResult"))
                {
                    UI_SearchResult();
                }
                else if (Track.Equals("InsertEditDetails"))
                {
                    PLServiceTax objServTax = new PLServiceTax();
                    objServTax.MstServiceTaxDetailID = TrynParse.parseInt(ClientRequest["ServiceTaxDetailID"]);
                    objServTax.ServiceTaxID = TrynParse.parseInt(ClientRequest[cboAddServiceTax.ID]);
                    objServTax.ServiceTaxName = ClientRequest[txtServiceTaxName.ID]; 
                    objServTax.ServiceTaxPer = TrynParse.parseDecimal(ClientRequest[txtServiceTaxPer.ID]);
                    objServTax.SortOrder = TrynParse.parseInt(ClientRequest[txtSortOrder.ID]);
                    objServTax.IsActive = TrynParse.parseBool(ClientRequest[chkIsActive.ID]);
                    try
                    {
                        int Result = new BL_ServiceTax().BL_InsertUpdateServiceTaxDetails(objServTax);
                        if (Result > 0)
                        {
                            ClientResponse["strSaveMessage"] = GlobalDefaultValues.SaveMessage.Replace("XXX", "Service Tax Detail");
                            ClientResponse["strUpdateMessage"] = GlobalDefaultValues.UpdateMessage.Replace("XXX", "Service Tax Detail");
                            ClientResponse["IsSuccess"] = "1";
                            UI_SearchResult();
                        }
                        else if (Result == -1)
                        {
                            ClientResponse["IsSuccess"] = "-1";
                            ClientResponse["strMessage"] = "Service Tax Name duplicate";
                        }
                        else
                        {
                            ClientResponse["IsSuccess"] = "0";
                            ClientResponse["strMessage"] = GlobalDefaultValues.SaveErrorMessage.Replace("XXX", "Service Tax Detail");
                        }
                    }
                    catch (Exception ex)
                    {
                        ClientResponse["strMessage"] = ex.Message;
                        CommonUtil.Elmah_SignalException(ex, this.Context, "ServiceTaxDetail - Method: RaiseCallbackEvent(), Track-InsertEditDetails");
                        ClientResponse["IsSuccess"] = "0";
                    }

                }
            }
        }
        catch (Exception ex)
        {
            CommonUtil.Elmah_SignalException(ex, this.Context, "Service Tax Detail - Method: RaiseCallbackEvent()");
        }
    }


    private void UI_SearchResult()
    {
        PLServiceTax objServTax = new PLServiceTax();
        if (ClientRequest[cboStatus.ID] == "2")
            objServTax.SearchIsActive = null;
        else
        {
            if (ClientRequest[cboStatus.ID] == "0")
                objServTax.SearchIsActive = false;
            else
                objServTax.SearchIsActive = true;
        }
        if (ClientRequest[cboServiceTax.ID] == "0")
            objServTax.SearchServiceTaxID = null;
        else
            objServTax.SearchServiceTaxID = TrynParse.parseInt(ClientRequest[cboServiceTax.ID]);

        grdResult.DataSource = new BL_ServiceTax().BL_fnGetServiceTaxDetailList(new PLServiceTax());
        grdResult.DataBind();
        ClientResponse["grdResult"] = CommonUtil.GetRenderControl(grdResult);
    }

    protected void grdResult_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Style["display"] = "none";
            e.Row.Cells[1].Style["display"] = "none";
            e.Row.Cells[2].Text = (e.Row.RowIndex + 1).ToString();
        }
        else if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Style["display"] = "none";
            e.Row.Cells[1].Style["display"] = "none";
        }
    }
    #endregion

}